import request from '@/utils/request'
import type { ApiResponse, PageResponse } from '@/types/api'

export interface ShareLinkVO {
  id: string
  fileId: string
  shareCode: string
  shareType: number
  password?: string
  validUntil?: string
  maxAccessCount?: number
  maxDownloadCount?: number
  accessCount: number
  downloadCount: number
  status: number
  description?: string
  createTime?: string
}

export interface CreateShareLinkRequest {
  fileId: string
  shareType?: number
  password?: string
  validUntil?: string
  maxAccessCount?: number
  maxDownloadCount?: number
  description?: string
}

export interface UpdateShareLinkRequest {
  shareType?: number
  password?: string
  validUntil?: string
  maxAccessCount?: number
  maxDownloadCount?: number
  description?: string
}

export const shareApi = {
  create: (req: CreateShareLinkRequest): Promise<ApiResponse<ShareLinkVO>> =>
    request.post('/file/share/create', req),

  infoByCode: (shareCode: string): Promise<ApiResponse<ShareLinkVO>> =>
    request.get(`/file/share/info/${shareCode}`),

  validate: (shareCode: string, password?: string): Promise<ApiResponse<ShareLinkVO>> =>
    request.post('/file/share/validate', { shareCode, password }),

  recordDownload: (shareCode: string): Promise<ApiResponse<void>> =>
    request.post(`/file/share/download/${shareCode}`),

  listByFile: (fileId: string): Promise<ApiResponse<ShareLinkVO[]>> =>
    request.get(`/file/share/list/${fileId}`),

  listValidByFile: (fileId: string): Promise<ApiResponse<ShareLinkVO[]>> =>
    request.get(`/file/share/valid/${fileId}`),

  myShares: (current = 1, size = 10): Promise<ApiResponse<PageResponse<ShareLinkVO>>> =>
    request.get('/file/share/my', { params: { current, size } }),

  update: (shareId: string, req: UpdateShareLinkRequest): Promise<ApiResponse<void>> =>
    request.put(`/file/share/${shareId}`, req),

  disable: (shareId: string): Promise<ApiResponse<void>> =>
    request.put(`/file/share/disable/${shareId}`),

  enable: (shareId: string): Promise<ApiResponse<void>> =>
    request.put(`/file/share/enable/${shareId}`),

  delete: (shareId: string): Promise<ApiResponse<void>> =>
    request.delete(`/file/share/${shareId}`),

  deleteAllByFile: (fileId: string): Promise<ApiResponse<void>> =>
    request.delete(`/file/share/all/${fileId}`),

  statsByShare: (shareId: string): Promise<ApiResponse<Record<string, any>>> =>
    request.get(`/file/share/stats/${shareId}`),

  statsMy: (days = 30): Promise<ApiResponse<Record<string, any>>> =>
    request.get('/file/share/stats/my', { params: { days } }),

  popular: (limit = 10, days = 7): Promise<ApiResponse<ShareLinkVO[]>> =>
    request.get('/file/share/popular', { params: { limit, days } }),

  adminProcessExpired: (): Promise<ApiResponse<number>> =>
    request.post('/file/share/admin/process-expired'),

  adminProcessLimitReached: (): Promise<ApiResponse<number>> =>
    request.post('/file/share/admin/process-limit-reached')
}

export default shareApi


